package com.example.hostsys.controller;

import com.example.hostsys.entity.User;
import com.example.hostsys.service.UserService;
import com.example.hostsys.util.ResultSet;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
public class HomeController {

    final UserService userService;

    @Autowired
    public HomeController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/")
    public String index(Authentication auth){
        if(auth!=null) {
            return "redirect:/user/home";
        }
        return "index";
    }
    @GetMapping("/login")
    public String login(Authentication auth){
        if(auth!=null) {
            return "redirect:/user/home";
        }
        return "login";
    }
    @GetMapping("/signup")
    public String register(){
        return "signup";
    }

    @ApiOperation(value = "注册", notes = "", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "{username,password,name,gender}", paramType = "json", required = true, dataType = "json")
    })

    @PostMapping("/signup")
    @ResponseBody
    public ResultSet signUp(@RequestBody User user) {
        if(userService.isExist(user.getUsername())){
            return ResultSet.fail();
        }
        else if(user.getName()==null||user.getGender()==null){
            return ResultSet.errorMsg("请填写姓名/性别");
        }
        userService.newUser(user);
        return ResultSet.success();
    }
}
